package com.sth.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sth.base.exceptionHandler.SthException;
import com.sth.edu.entity.Course;
import com.sth.edu.entity.CourseDescription;
import com.sth.edu.entity.vo.CourseFrontVo;
import com.sth.edu.entity.vo.CourseInfoVo;
import com.sth.edu.entity.vo.CoursePublishVo;
import com.sth.edu.entity.vo.CourseWebVo;
import com.sth.edu.mapper.CourseMapper;
import com.sth.edu.service.ChapterService;
import com.sth.edu.service.CourseDescriptionService;
import com.sth.edu.service.CourseService;
import com.sth.edu.service.VideoService;
import com.sth.edu.utils.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author sth
 * @since 2020-12-05
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ChapterService chapterService;


    /**
     * 添加课程信息
     *
     * @param courseInfoVo
     */
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1.向课程表 添加课程信息
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo, course);
        course.setId(UUIDUtil.get4UUID());
        int insert = baseMapper.insert(course);
        if (insert <= 0) {
            throw new SthException(20001, "添加课程失败");
        }
        //2.向课程简介中添加
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(course.getId());
        courseDescriptionService.save(courseDescription);
        return course.getId();
    }

    @Override
    public CourseInfoVo getCourseInfo(String coureseId) {
        // 1. 课程表
        Course course = baseMapper.selectById(coureseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course, courseInfoVo);

        //2. 描述表
        CourseDescription courseDescription = courseDescriptionService.getById(course.getId());
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        // 1.修改课程表
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo, course);
        int i = baseMapper.updateById(course);
        if (i == 0) {
            throw new SthException(2001, "修改课程失败");
        }
        // 2. 描述表
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setId(courseInfoVo.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(courseDescription);

    }

    // 根据课程id 查询课程确认的信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        // 调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    // 删除课程
    @Override
    public void removeCourse(String courseId) {
        // 1. 删除小节
        videoService.removeVideoByCourseId(courseId);
        // 2. 删除章节
        chapterService.removeChapterByCourseId(courseId);
        // 3. 删除描述
        courseDescriptionService.removeById(courseId);
        // 4. 删除本身
        int result = baseMapper.deleteById(courseId);
        if (result == 0) {
            throw new SthException(20001, "删除课程,删除本身失败");
        }
    }
    // 条件查询带分页
    @Override
    public Map<String, Object> getCourseFrontList(Page<Course> pageTecher, CourseFrontVo courseFrontVo) {
        //2 根据讲师id查询所讲课程
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageTecher,wrapper);

        List<Course> records = pageTecher.getRecords();
        long current = pageTecher.getCurrent();
        long pages = pageTecher.getPages();
        long size = pageTecher.getSize();
        long total = pageTecher.getTotal();
        boolean hasNext = pageTecher.hasNext();//下一页
        boolean hasPrevious = pageTecher.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }

    //根据课程id，编写sql语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
